package com.lmf.eduservice.controller;


import com.lmf.eduservice.entity.EduChapter;
import com.lmf.eduservice.entity.vo.ChapterVo;
import com.lmf.eduservice.service.EduChapterService;
import com.lmf.handler.Exception.LmfException;
import com.lmf.resultCode.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lmf
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
@Api(description = "章节获取管理")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @GetMapping("/getChapterVideo/{courseId}")
    @ApiOperation("根据课程id，获取所有的章节数据")
    public R getChapterVideo(@PathVariable("courseId") String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation("修改章节")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation("根据章节id，查找某个章节")
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable("chapterId") String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    @ApiOperation("根据章节id，删除章节")
    @DeleteMapping("/deleteChapterById/{chapterId}")
    public R deleteChapterById(@PathVariable("chapterId") String chapterId) throws LmfException {
        boolean flag = eduChapterService.deleteChapterById(chapterId);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }
}

